package com.niiwoo.civet.trade.service.dubbo.intelligenceInvest;

import com.alibaba.dubbo.common.utils.CollectionUtils;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.niiwoo.civet.trade.dao.entity.ProjectPackageInvestOrder;
import com.niiwoo.civet.trade.dto.common.InvestedOrderDTO;
import com.niiwoo.civet.trade.dto.request.ProjectInvestOrderPageRequest;
import com.niiwoo.civet.trade.dto.request.intelligenceInvest.IntelligenceDetailListRequestDTO;
import com.niiwoo.civet.trade.dto.request.intelligenceInvest.IntelligenceOrderDetailRequestDTO;
import com.niiwoo.civet.trade.dto.request.intelligenceInvest.IntelligenceOrderListRequestDTO;
import com.niiwoo.civet.trade.dto.response.InvestOrderMaxandMinDateResponseDTO;
import com.niiwoo.civet.trade.dto.response.ProjectInvestOrderPageResponse;
import com.niiwoo.civet.trade.dto.response.intelligenceInvest.IntelligenceDetailListResponseDTO;
import com.niiwoo.civet.trade.dto.response.intelligenceInvest.IntelligenceInvestOrderDTO;
import com.niiwoo.civet.trade.dto.response.intelligenceInvest.IntelligenceOrderDetailResponseDTO;
import com.niiwoo.civet.trade.dto.response.intelligenceInvest.ProjectPackageInvestOrderDTO;
import com.niiwoo.civet.trade.enums.ChannelCodeEnum;
import com.niiwoo.civet.trade.service.intelligenceInvest.IntelligenceOrderDubboService;
import com.niiwoo.civet.trade.service.local.intelligenceInvest.IntelligenceOrderService;
import com.niiwoo.civet.user.dto.request.SimpleOrgInfo;
import com.niiwoo.civet.user.dto.response.UserBasicInfoDTO;
import com.niiwoo.civet.user.service.OrganizationDubboService;
import com.niiwoo.civet.user.service.UserBasicInfoDubboService;
import com.niiwoo.tripod.base.enums.UserTypeEnum;
import com.niiwoo.tripod.provider.dto.response.PageResponseDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;

import java.util.*;

@Slf4j
@Service(version = "1.0.0")
public class IntelligenceOrderDubboServiceImpl implements IntelligenceOrderDubboService {

    @Autowired
    IntelligenceOrderService intelligenceOrderService;

    @Reference(version = "1.0.0")
    private OrganizationDubboService organizationDubboService;

    @Reference(version = "1.0.0")
    private UserBasicInfoDubboService userBasicInfoDubboService;


    @Override
    public PageResponseDTO<IntelligenceInvestOrderDTO> intelligenceOrderList(IntelligenceOrderListRequestDTO requestDTO) {
        Assert.notNull(requestDTO, "请求参数不能为空");
        Assert.notNull(requestDTO.getUserId(), "用户ID不能为空");
        Assert.notNull(requestDTO.getType(), "订单类型不能为空");
        return intelligenceOrderService.intelligenceOrderList(requestDTO);
    }

    @Override
    public IntelligenceOrderDetailResponseDTO intelligenceOrderDetail(IntelligenceOrderDetailRequestDTO requestDTO) {
        Assert.notNull(requestDTO, "请求参数不能为空");
        Assert.notNull(requestDTO.getPackageInvestOrderId(), "包投资订单ID不能为空");
        return intelligenceOrderService.intelligenceOrderDetail(requestDTO);
    }

    @Override
    public PageResponseDTO<IntelligenceDetailListResponseDTO> intelligenceDetailList(IntelligenceDetailListRequestDTO requestDTO) {
        Assert.notNull(requestDTO, "请求参数不能为空");
        Assert.notNull(requestDTO.getPackageInvestOrderId(), "包投资订单ID不能为空");
        return intelligenceOrderService.intelligenceDetailList(requestDTO);
    }

    @Override
    public ProjectInvestOrderPageResponse getPageIntelligenceInvestOrderList(ProjectInvestOrderPageRequest request) {
        ProjectInvestOrderPageResponse response = new ProjectInvestOrderPageResponse();
        List<InvestedOrderDTO> dataList = new ArrayList<>();
        int totalRecord = intelligenceOrderService.countInvested(request.getProjectId());
        if (totalRecord > 0) {
            List<ProjectPackageInvestOrder> investors = intelligenceOrderService.getIntelligenOrderList(request);
            Set<String> userIdSet = new HashSet<>();
            Set<String> orgIdSet = new HashSet<>();
            for (ProjectPackageInvestOrder projectInvestOrder : investors) {
                if(UserTypeEnum.ORGANIZATION.getValue().equals(projectInvestOrder.getInvestorType())) {
                    orgIdSet.add(projectInvestOrder.getUserId());
                }else {
                    userIdSet.add(projectInvestOrder.getUserId());
                }
            }
            Map<String, UserBasicInfoDTO> userBasicInfoDTOMap = new HashMap<>();
            Map<String, SimpleOrgInfo> orgInfoDTOMap = new HashMap<>();
            if (CollectionUtils.isNotEmpty(userIdSet)) {
                List<UserBasicInfoDTO> userBasicInfoDTOS = userBasicInfoDubboService.queryListByUserIdSet(userIdSet);
                for (UserBasicInfoDTO userBasicInfoDTO : userBasicInfoDTOS) {
                    userBasicInfoDTOMap.put(userBasicInfoDTO.getUserId(), userBasicInfoDTO);
                }
            }
            if(CollectionUtils.isNotEmpty(orgIdSet)) {
                List<SimpleOrgInfo> orgs = organizationDubboService.queryListByOrgIdSet(orgIdSet);
                for (SimpleOrgInfo simpleOrgInfo : orgs) {
                    orgInfoDTOMap.put(simpleOrgInfo.getId()+"", simpleOrgInfo);
                }
            }
            //企业投资人与个人投资区分
            for (ProjectPackageInvestOrder projectInvestOrder : investors) {
                InvestedOrderDTO dto = new InvestedOrderDTO();
                if(UserTypeEnum.PERSONAL.getValue().equals(projectInvestOrder.getInvestorType())) {
                    UserBasicInfoDTO userBasicInfoDTO = userBasicInfoDTOMap.get(projectInvestOrder.getUserId());
                    dto.setInvestorHeadImage(userBasicInfoDTO.getHeadImage());
                    dto.setInvestorGender(userBasicInfoDTO.getGender());
                    dto.setInvestorNickName(userBasicInfoDTO.getNickName());
                    dto.setInvestorNiiwooScore(userBasicInfoDTO.getNiiwooScore());
                }else {
                    SimpleOrgInfo info = orgInfoDTOMap.get(projectInvestOrder.getUserId());
                    dto.setInvestorHeadImage(info.getHeadImg());
                    dto.setInvestorGender(Byte.valueOf("1"));
                    dto.setInvestorNickName(info.getNickName());
                    dto.setInvestorNiiwooScore(300);
                }
                dto.setInvestUserId(projectInvestOrder.getUserId());
                dto.setAmount(projectInvestOrder.getRealInvestAmount());
                dto.setInvestTime(projectInvestOrder.getCreateTime());
                dto.setChannelCode(ChannelCodeEnum.APP.getFinanceFlag());
                dataList.add(dto);
            }
        }
        response.setTotalRecord(totalRecord);
        response.setDataList(dataList);
        return response;
    }

    @Override
    public InvestOrderMaxandMinDateResponseDTO getMyIntelligenceInvestOrderMaxAndMinDate(String userId, Byte type){
        return intelligenceOrderService.getMyIntelligenceInvestOrderMaxAndMinDate(userId,type);
    }
    @Override
    public ProjectPackageInvestOrderDTO getProjectPackageInvestOrderBySubInvestOrderId(Long subInvestId) {
    	 
    	return intelligenceOrderService.getProjectPackageInvestOrderBySubInvestOrderId(subInvestId);
    }
}
